package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.*;
import com.sky.entity.Food;
import com.sky.mapper.FoodMapper;
import com.sky.result.PageResult;
import com.sky.service.FoodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Override
    public List<Food> getAll() {
        List<Food> foodList = foodMapper.foodList();
        System.out.println(foodList);
        return foodList;
    }

    @Override
    public PageResult pageQuery(FoodPageQueryDTO foodPageQueryDTO) {
        IPage iPage=new Page(foodPageQueryDTO.getPage(),foodPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Food> lam=new LambdaQueryWrapper<>();
        //添加过滤条件
        if(foodPageQueryDTO.getFoodName()!=null&&foodPageQueryDTO.getFoodName()!=""){
            lam.like(Food::getFoodName,foodPageQueryDTO.getFoodName());
        }
        if(foodPageQueryDTO.getStatus()!=null&&foodPageQueryDTO.getStatus()!=3){
            lam.eq(Food::getStatus,foodPageQueryDTO.getStatus());
        }
        if(foodPageQueryDTO.getCategory()!=null&&foodPageQueryDTO.getCategory()!=0){
            lam.eq(Food::getCategory,foodPageQueryDTO.getCategory());
        }
        if(foodPageQueryDTO.getOperator()!=null&&foodPageQueryDTO.getOperator()!=""){
            lam.like(Food::getOperator,foodPageQueryDTO.getOperator());
        }
        foodMapper.selectPage(iPage,lam);
        long total=iPage.getTotal();
        List<Food> foodList=iPage.getRecords();
        PageResult pageResult=new PageResult(total,foodList);
        return pageResult;
    }

    @Override
    public List<Food> getByCategory(Long category) {
        List<Food> foodList=foodMapper.foodListByCategory(category);
        return foodList;
    }

    @Override
    public List<Food> getByName(FoodNameDTO foodName) {
        List<Food> foodList=foodMapper.foodListByName(foodName);
        return foodList;
    }

    @Override
    public void addHistory(HistoryDTO historyDTO) {
        foodMapper.addHistory(historyDTO);
    }

    @Override
    public List<String> getHistoryById(HistoryDTO historyDTO) {
        List<String> getHistoryById=foodMapper.getHistoryById(historyDTO);
        return getHistoryById;
    }

    @Override
    public List<SuggestionsDTO> getSuggestions(SuggestionsDTO suggestionsDTO) {
        List<SuggestionsDTO> getSuggestions = foodMapper.getSuggestions(suggestionsDTO);
        return getSuggestions;
    }

    @Override
    public void startOrStop(Long status, Long id) {
        LambdaQueryWrapper<Food> lam=new LambdaQueryWrapper<>();
        Food food=new Food();
        food.setStatus(status);
        food.setId(id);
        foodMapper.updateById(food);
    }

    @Override
    public void addFood(AddFoodDTO addFoodDTO) {
        Food food=new Food();
        //对象属性拷贝
        BeanUtils.copyProperties(addFoodDTO,food);
        foodMapper.insert(food);
    }

    @Override
    public Food selectById(Long id) {
        Food food = foodMapper.selectById(id);
        return food;
    }

    @Override
    public void updateFood(UpdateFoodDTO updateFoodDTO) {
        Food food=new Food();
        BeanUtils.copyProperties(updateFoodDTO,food);
        foodMapper.updateById(food);
    }

    @Override
    public void userAddFood(AddFoodDTO addFoodDTO) {
        Food food=new Food();
        //对象属性拷贝
        BeanUtils.copyProperties(addFoodDTO,food);
        food.setStatus(0L);
        foodMapper.insert(food);
    }

    @Override
    public void deleteFood(Long id) {
        foodMapper.deleteById(id);
    }

    @Override
    public Food getFoodById(Long id) {
        Food food = foodMapper.selectById(id);
        return food;
    }

    @Override
    public Integer getCount() {
        LambdaQueryWrapper<Food> lam=new LambdaQueryWrapper<>();
        return foodMapper.selectCount(lam);
    }

}
